//
//  PlayGhostLayer.cpp
//  NewMath
//
//  Created by Ngô Hoàng Nam on 1/3/15.
//
//

#include "PlayGhostLayer.h"
#include "../PlayManager.h"
//#include "../../../Objects/Ghost/Play/PlaySelectGhostObj.h"
#include <cocostudio/CocoStudio.h>
#include "../../Level/LevelManager.h"
#include "../../../Data/GameData.h"

//#include "../Object/PlayAdjustGhostObj.h"
PlayGhostLayer::PlayGhostLayer(Size screenSize)
{
	m_screenSize = screenSize;
}


void PlayGhostLayer::initAdjustPlayOneCol(cocos2d::Node *nodeRoot, int numGhost, int visibleGhost)
{
    Sprite* bg = Sprite::create("bg_easy.png");
    float bgScale = m_screenSize.height/bg->getContentSize().height;
    
    float variance = (bg->getContentSize().width*bgScale - m_screenSize.width)/2;
    int numRow = 0;
    
    if(numGhost < 4)
        numRow = 1;
    else if(numGhost < 9)
        numRow = 2;
    else if(numGhost < 16)
        numRow = 3;
    else if(numGhost < 41)
        numRow = 4;
    else
        numRow = 5;
    
    float numCol = (float)numGhost/(float)numRow;
    if(numCol > (int)numCol)
        numCol = (int)numCol+ 1;
    float scaleAdjust =(float)(numRow)/100.0f;
    float newScreenHeight = m_screenSize.height - m_screenSize.height*(0.5f - scaleAdjust);
    
    float canvasHeight = newScreenHeight / (numRow);
    if(canvasHeight == newScreenHeight)
        canvasHeight/=2;

    float expectHeight = canvasHeight*0.8f;
    float scale = expectHeight/kGhostSpriteHeight;
    
    
    float newGhostWidth = kGhostSpriteWidth*scale;
    float newGhostHeight = kGhostSpriteHeight*scale;
    
    float m_paddingHeight = canvasHeight - expectHeight;
    
    float m_paddingWidth = m_screenSize.width / (numCol + 1);
    float space = (m_screenSize.width - (newGhostWidth * numCol) - (m_paddingWidth * 2)) / (numCol - 1);
    
    Node* nodePanel = Node::create();
    nodePanel->setPosition(variance,newScreenHeight);
    nodePanel->setAnchorPoint(Vec2(0,1.0f));
    Vec2 vec = nodePanel->convertToWorldSpace(Vec2::ZERO);
    nodeRoot->addChild(nodePanel);
    
    EGhostColor ghostColor1 = GhostObject::GetRandomBody();
    EGhostColor ghostColor2;
    do
    {
        ghostColor2 = GhostObject::GetRandomBody();
    }while (ghostColor2 == ghostColor1);

    
    
    
    for (int y = 0,indexGhost = 1; y < numRow; y++)
    {
        if(numGhost < numCol)
        {
            m_paddingWidth = m_screenSize.width / (numGhost + 1);
            space = (m_screenSize.width - (newGhostWidth * numGhost) - (m_paddingWidth * 2)) / (numGhost - 1);
        }
        else
        {
            if(y % 2== 0)
                m_paddingWidth -= space/2;
            else
                m_paddingWidth += space/2;
        }
        for (int x = 0; x < numCol; x++,indexGhost++)
        {
            Node* nodeGhost = Node::create();
            
            float varianceX = random(-(int)(newGhostWidth*0.2f),(int)(newGhostWidth*0.2f));
            
            float varianceY = random(-(int)(newGhostHeight*0.2f),(int)(newGhostHeight*0.2f));
            
            
            Vec2 position = Vec2(m_paddingWidth + newGhostWidth / 2 + x * newGhostWidth + (x * space) + varianceX,
                                 newGhostHeight / 2 - y * newGhostHeight - (y * m_paddingHeight) + varianceY);
            
            
            nodeGhost->setPosition(position);
            
            nodePanel->addChild(nodeGhost);
            
            GhostAdjust* ghost = new GhostAdjust();
            
            
            
            
            if(indexGhost <= visibleGhost)
            {
                ghost->initalize(ghostColor1,EGhostFaceState::kGhostFaceState_Full,EGhostHandState::kGhostHandState_None,  Vec2::ZERO, scale, true,true);
                nodeGhost->addChild(ghost);
            }
            else
            {
                ghost->initalize(ghostColor2,EGhostFaceState::kGhostFaceState_Full,EGhostHandState::kGhostHandState_None,  Vec2::ZERO, scale, true,true);
                nodeGhost->addChild(ghost);
                ghost->setVisible(false);
            }
            numGhost--;
            if(numGhost == 0)
                return;
            
        }
        
    }
    nodeRoot->setVisible(true);

}


void PlayGhostLayer::initAdjustPlayTwoCol(cocos2d::Node *nodeRoot, int numGhostCol1,int numGhostCol2, int visibleGhostCol1,int visibleGhostCol2)
{
    Sprite* bg = Sprite::create("bg_easy.png");
    float bgScale = m_screenSize.height/bg->getContentSize().height;
    
    float variance = (bg->getContentSize().width*bgScale - m_screenSize.width)/2;
    
    EGhostColor ghostColor1 = GhostObject::GetRandomBody();
    EGhostColor ghostColor2;
    do
    {
        ghostColor2 = GhostObject::GetRandomBody();
    }while(ghostColor2 == ghostColor1);
    

#pragma region Col 1
    int numRow = 0;
    
    if(numGhostCol1 < 3)
        numRow = 1;
    else if(numGhostCol1 < 7)
        numRow = 2;
    else if(numGhostCol1 < 10)
        numRow = 3;
    else if(numGhostCol1 < 13)
        numRow = 4;
    else
        numRow = 5;
    
    float numCol = (float)numGhostCol1/(float)numRow;
    if(numCol > (int)numCol)
        numCol = (int)numCol+ 1;
    
    float newScreenHeight = m_screenSize.height - m_screenSize.height*0.5;
    
    float canvasHeight = newScreenHeight / (numRow);
    if(canvasHeight == newScreenHeight)
        canvasHeight/=2;
    Node* nodePanel1 = Node::create();
    nodePanel1->setPosition(variance,numRow * canvasHeight);
    nodePanel1->setAnchorPoint(Vec2(0,1.0f));
    nodeRoot->addChild(nodePanel1);
    
    initPanelGhost(nodePanel1, m_screenSize.width/2, newScreenHeight, numRow, numCol, numGhostCol1,visibleGhostCol1,ghostColor1);
#pragma endregion
    
#pragma region Col 2
    numRow = 0;
    
    if(numGhostCol2 < 3)
        numRow = 1;
    else if(numGhostCol2 < 7)
        numRow = 2;
    else if(numGhostCol2 < 10)
        numRow = 3;
    else if(numGhostCol2 < 13)
        numRow = 4;
    else
        numRow = 5;
    
    numCol = (float)numGhostCol2/(float)numRow;
    if(numCol > (int)numCol)
        numCol = (int)numCol+ 1;
    canvasHeight = newScreenHeight / (numRow);
    if(canvasHeight == newScreenHeight)
        canvasHeight/=2;
       Node* nodePanel2 = Node::create();
    nodePanel2->setPosition(m_screenSize.width/2  + variance,numRow * canvasHeight);
    nodePanel2->setAnchorPoint(Vec2(0,1.0f));
    nodeRoot->addChild(nodePanel2);
//
    initPanelGhost(nodePanel2, m_screenSize.width/2, newScreenHeight, numRow, numCol, numGhostCol2,visibleGhostCol2,ghostColor2);
#pragma endregion

    
    
    
}
void PlayGhostLayer::initPanelGhost(Node* nodePanel,float panelWidth,float panelHeight, int numRow, int numCol, int numGhost, int visibleGhost,EGhostColor ghostColor)
{
    
    float canvasHeight = panelHeight / numRow ;
    float expectHeight = numRow == 1? canvasHeight*0.5f : canvasHeight*0.7f;

    
    float scale = expectHeight/kGhostSpriteHeight;
    
    
    float newGhostWidth = kGhostSpriteWidth*scale;
    float newGhostHeight = kGhostSpriteHeight*scale;
    
    float m_paddingHeight = canvasHeight - expectHeight;
    
    float m_paddingWidth = panelWidth / (numCol + 1);
    float space = (panelWidth - (newGhostWidth * numCol) - m_paddingWidth) / (numCol > 1? numCol - 1 : 1);

    for (int y = 0,indexGhost = 1; y < numRow; y++)
    {
        if(numGhost < numCol)
        {
            m_paddingWidth = panelWidth / (numGhost + 1);
            space = (panelWidth - (newGhostWidth * numGhost) - m_paddingWidth*1.3f) / (numGhost > 1? numGhost - 1 : 1);
        }
        else
        {
            if(y % 2== 0)
                m_paddingWidth -= space/2;
            else
                m_paddingWidth += space/2;
        }
        for (int x = 0; x < numCol; x++,indexGhost++)
        {
            Node* nodeGhost = Node::create();
            
            //float varianceX = random(-(int)(newGhostWidth*0.2f),(int)(newGhostWidth*0.2f));
            
            float varianceY = random(-(int)(newGhostHeight*0.2f),(int)(newGhostHeight*0.2f));
            
            
            Vec2 position = Vec2(m_paddingWidth + newGhostWidth / 2 + x * newGhostWidth + (x * space),
                                 newGhostHeight / 2 - y * newGhostHeight - (y * m_paddingHeight) + varianceY);
            
            
            nodeGhost->setPosition(position);
            
            nodePanel->addChild(nodeGhost);
            
            GhostAdjust* ghost = new GhostAdjust();
            
            
            
          
            ghost->initalize(ghostColor,EGhostFaceState::kGhostFaceState_Full,EGhostHandState::kGhostHandState_None,  Vec2::ZERO, scale,true,true);
 
            nodeGhost->addChild(ghost);
            if(indexGhost > visibleGhost)
                ghost->setVisible(false);
            numGhost--;
            if(numGhost == 0)
                return;
            
        }
        
    }
    nodePanel->setVisible(true);
}
void PlayGhostLayer::initalizeSelectPlay(Node* nodeRoot, int correctValue)
{
	int numRow = (int)nodeRoot->getChildrenCount();
	float canvasHeight = m_screenSize.height /  (numRow + 1); // 1 dong cua header
    float expectHeight = canvasHeight*0.7f;
    float scale = expectHeight/kGhostSpriteHeight;
	//int numGhost = cocos2d::random(0,(int)nodeRoot->getChildByName("nodeNormal")->getChildrenCount());

	int ghostCount = 0;
	for (int i = 0; i < numRow; i++)
	{
		Node* row = nodeRoot->getChildByName(cocos2d::String::createWithFormat("%s%i", "Row_", i + 1)->getCString());
		int numCol = (int)row->getChildrenCount();
		for (int j = 0; j < numCol; j++)
		{
			ghostCount++;
		}
	}
    
    int* arrValue = new int[ghostCount];

	int minRandom = 1;
	int maxRandom = correctValue * 1.5f;
	arrValue[0] = random(minRandom, maxRandom);
	for (int i = 1; i < ghostCount;)
	{
		int val = random(minRandom, maxRandom);
		bool isOK = true;
		for(int j= 0; j< ghostCount;j++)
		{
			if (arrValue[j] == val || val == correctValue)
			{
				isOK = false;
				break;
			}
		}
		if (isOK)
		{
			arrValue[i] = val;
			i++;
		}
			

           
	}
    
	int posAnswer = cocos2d::random(1, ghostCount);

	for (int i = 0,indexGhost = 1; i < numRow; i++)
	{
		Node* row = nodeRoot->getChildByName(cocos2d::String::createWithFormat("%s%i", "Row_", i + 1)->getCString());
		int numCol = (int)row->getChildrenCount();
		for (int j = 0; j < numCol; j++,indexGhost++)
		{
			GhostSelect* ghost = new GhostSelect();
            //auto nodeName = cocos2d::String::createWithFormat("%s%d", "ghost_", indexGhost)->getCString();
            //Vec2 pos = row->getChildByName(nodeName)->getPosition();
			switch (LevelManager::s_mode) {
			case kMode_Easy:
                   
				if (indexGhost == posAnswer)
				{
					
                    ghost->initalize(EGhostColor::kGhostColor_Blue, Vec2::ZERO, scale, correctValue, true,true);
				}
				else
				{
                    ghost->initalize(EGhostColor::kGhostColor_Blue, Vec2::ZERO, scale, arrValue[indexGhost -1], true,true);
				}
				break;
			case kMode_Normal:
                    if (indexGhost == posAnswer)
                    {
                        
                        ghost->initalize(EGhostColor::kGhostColor_Cyan, Vec2::ZERO, scale, correctValue, true,true);
                    }
                    else
                    {
                        ghost->initalize(EGhostColor::kGhostColor_Cyan, Vec2::ZERO, scale, arrValue[indexGhost -1], true,true);
                    }
				break;
			case kMode_Hard:
                    if (indexGhost == posAnswer)
                    {
                        ghost->initalize(EGhostColor::kGhostColor_Purple, Vec2::ZERO, scale, correctValue, true,true);
                    }
                    else
                    {
                        ghost->initalize(EGhostColor::kGhostColor_Purple, Vec2::ZERO, scale, arrValue[indexGhost -1], true,true);
                    }
				break;
			default:
				break;

			}
            Node* nodeGhost= row->getChildByName(cocos2d::String::createWithFormat("%s%d", "ghost_", indexGhost)->getCString());
            nodeGhost->addChild(ghost);
		}
		
	}
	nodeRoot->setVisible(true);
}

